<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\models\Article;
use app\models\Banner;
use yii;


class BannerLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getBanner($query = [], $page = 1, $limit = 10)
    {
        $banner = Banner::find()->filterWhere($query)->andWhere("is_del=0")
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $banner->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $banner->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'banner/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getById($id)
    {
        return Banner::find()->where(['banner_id' => $id])->asArray()->one();
    }


    public function saveStatus($id)
    {
        $banner = Banner::findOne($id);
        if($banner->status)
            $banner->status = 0;
        else
            $banner->status = 1;
        return $banner->save();
    }

    public function addBanner($params,$uploadresult){
        $banner = new Banner();
        $banner->banner_type = $params['banner_type'];
        $banner->banner_title = $params['banner_title'];
        $banner->banner_pic = $uploadresult;
        $banner->add_time = date('Y-m-d H:i:s');
        return $banner->save();
    }

    public function editById($id, $params,$uploadresult){
        if(!$banner = Banner::findOne($id)) return false;
        $banner->banner_type = $params['banner_type'];
        $banner->banner_title = $params['banner_title'];
        if($uploadresult)
            $banner->banner_pic = $uploadresult;
        return $banner->save();
    }

    public function delete($id){
        if(!$banner = Banner::findOne($id)) return false;
        $banner->is_del = 1;
        return $banner->save();
        /*return $banner->delete();*/
    }



}